/*
 * JSFConsole - Lightweight JSF Application Monitor
 * Copyright (C) 2009  Grzegorz Bugaj
 * http://www.gregbugaj.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.gregbugaj.reflector.ui;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.el.ELException;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import com.gregbugaj.reflector.renderer.AbstractRenderer;
import com.gregbugaj.reflector.renderer.HtmlRenderer;
import com.gregbugaj.reflector.renderer.TreeRenderer;
import com.gregbugaj.reflector.visitors.XMLVisitor;

public class UISteppingStone extends UIComponentBase {
	public UISteppingStone() {
		super();
	}

	//Passed properties
	private String rendererType;	
	private Object object;
	
	public void setObject(Object object) {
		this.object = object;
	}

	//Current state
	private Object[] state;

	public String getRendererType() {
		if (rendererType != null) {
			return rendererType;
		}
		ValueExpression exp = this.getValueExpression("rendererType");
		if (exp != null) {
			try {
				return (String)exp.getValue(getFacesContext().getELContext());
			} catch (ELException ex) {
				throw new FacesException(ex);
			}
		}
		return null;
	}

	public Object getObject() {
		if (object != null) {
			return object;
		}
		ValueExpression exp = this.getValueExpression("object");
		if (exp != null) {
			try {
				return exp.getValue(getFacesContext().getELContext());
			} catch (ELException ex) {
				throw new FacesException(ex);
			}
		}
		return null;
	}
	

	@Override
	public void encodeBegin(FacesContext context) throws IOException {
		assertValidInput(context);
		if (!isRendered()) {
			return;
		}
		ResponseWriter writer=context.getResponseWriter();
		InputStream stream =null;	
		try {
			
			XMLVisitor visitor = new XMLVisitor();
			visitor.visit( this.getObject() );
			
			AbstractRenderer renderer=new TreeRenderer();
			renderer.render(visitor.getContext());			
			
			String template="/templates/template.txt";
			stream = this.getClass().getResourceAsStream(template);
				
			int result=-1;
			StringBuffer sb=new StringBuffer();
			while((result=stream.read())!=-1){
				sb.append((char)result);
			}
			
			String tempCode=sb.toString();
			tempCode = tempCode.replaceAll("%output%", renderer.getBufferToString());	
			writer.write(tempCode);

		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(stream != null){
				try {
					stream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	@Override
	public void encodeEnd(FacesContext context) throws IOException {
		assertValidInput(context);
		if (!isRendered()) {
			return;
		}
		super.encodeEnd(context);
		ResponseWriter writer=context.getResponseWriter();		
		//String script=" ";		
		//String id=this.getClientId(context);
	}



	@Override
	public String getFamily() {
		return null;
	}


	@Override
	public void restoreState(FacesContext context, Object state) {
		this.state=(Object[])state;

	}
	@Override
	public Object saveState(FacesContext context) {
		if(state==null){
			state=new Object[1];			
		}
		this.state[0] = super.saveState(context);
		return this.state;
	}


	/**
	 * This method checks for null context and component
	 * 
	 * @param context
	 * @param component
	 */
	private void assertValidInput(FacesContext context) {
		if (context == null) {
			throw new NullPointerException();
		} 
	}
}
